package com.lechi.iot.android.device;

import com.lechi.iot.android.channel.IIotTcpChannel;
import com.lechi.iot.android.service.NameValueImpl;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;

import java.net.InetSocketAddress;

/**
 * 设备组
 *
 * @author yangguang
 */
public class IotTcpDeviceChannel extends NameValueImpl implements IIotTcpChannel {

    private final Channel channel;

    private final String hostName;

    private String deviceSn;

    public IotTcpDeviceChannel(InetSocketAddress address, Channel channel) {
        this.channel = channel;
        this.hostName = address.getHostName() + ":" + address.getPort();
        name(address.getHostName() + ":" + address.getPort());
    }

    public IotTcpDeviceChannel(Channel channel) {
        this((InetSocketAddress) channel.remoteAddress(), channel);
    }

    @Override
    public String hostName() {
        return hostName;
    }

    public void send(ByteBuf msg) {
        channel.writeAndFlush(msg);
    }

    @Override
    public String getDeviceSn() {
        return deviceSn;
    }

    @Override
    public void setDeviceSn(String deviceSn) {
        this.deviceSn = deviceSn;
    }
}
